package Listener;

import org.eclipse.swt.SWT;
import org.eclipse.swt.graphics.GC;
import org.eclipse.swt.widgets.Canvas;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;

import Util.MODE;


public class DrawingListener implements Listener{

	private String[] mode;
	private String userID;
	private int lastX;
	private int lastY;
	
	public DrawingListener(String[] mode, String userID){
		this.userID = userID;
		this.mode = mode;
	}
	
	@Override
	public void handleEvent(Event event) {
		if(mode[MODE.MODE].equals(MODE.DRAW)){
			int size = event.widget.getDisplay().getShells().length;
			GC gc = new GC((Canvas)event.widget);
			switch(event.type){
			case SWT.MouseMove:
				if ((event.stateMask & SWT.BUTTON1) == 0) break;
				
				if(size > 1){
					for(int i = 0; i < size; i++ ){
						for(int j = 0; j < event.widget.getDisplay().getShells()[i].getChildren().length; j++){
							if(event.widget.getDisplay().getShells()[i].getChildren()[j] instanceof Canvas){
								gc = new GC((event.widget.getDisplay().getShells()[i]).getChildren()[j]);
								gc.setLineWidth(2);
								gc.drawLine(lastX, lastY, event.x, event.y);
								gc.drawString(userID + "             ", 10, 10);
							}
						}
					}
				}else{
					gc.drawLine(lastX, lastY, event.x, event.y);
				}
			case SWT.MouseDown:
				lastX = event.x;
				lastY = event.y;
				break;
			case SWT.MouseUp:
				gc.dispose();
				break;
			}
		}
	}

	
	
}
